<?php
defined('SYSPATH') or die('No direct script access.');

class Controller_Profile extends Controller_Initialize {

  public $secure_actions = array(
      // user actions
      // the others are public (forgot, login, register, reset, noaccess)
      );

    public function action_index() {

      $name = $this->request->param('username');
      /*$name = substr($name,0,2);
      echo Kohana::debug($name);
       */

      /*$str = 'lastName';
      $pattern = "/(.)([A-Z])/";
      $replacement = "\\1 \\2";
      echo Kohana::debug( ucfirst(preg_replace($pattern, $replacement, $str)));
       */

      $user = ORM::factory('user')->where('username', '=', $name)->find();
      $photo_user = ORM::factory('photo_user')->where('username', '=', $name)->find_all();
      $photo_set = ORM::factory('photo_set')->where('username', '=', $name)->find_all();

      $this->template->content = View::factory('templates/profile/index')
              ->bind('user', $user)
              ->bind('photo_set', $photo_set)
              ->bind('photo_user', $photo_user);
   
    }

  public function action_edit($id) {

    if (Auth::instance()->logged_in('admin')) {
      $model = ORM::factory('user')->find($id);
    } else {
      $model = ORM::factory('user')->where('username', '=', $this->userAuth->username)->find($id);
    }

    if (!empty($_POST)) {

      $model->text = Text::auto_link(strip_tags($_POST['description'],'<br><strong>'));
      $model->save();

      Message::add('success', '<strong>Změny byly uloženy.</strong>');

      Request::instance()->redirect('/'.$model->username);

    } else {

      $this->template->content = View::factory('templates/profile/edit')
                      ->bind('user', $model);
    }
  }

  public function action_edit_sets($name) {

    if (Auth::instance()->logged_in('admin')) {
      $user_photos = ORM::factory('photo_user')->where('username', '=', $name)->find_all();
      $photo_sets = ORM::factory('photo_set')->where('username', '=', $name)->find_all();
    } else {
      $user_photos = ORM::factory('photo_user')->where('username', '=', $this->userAuth->username)->find_all();
      $photo_sets = ORM::factory('photo_set')->where('username', '=', $this->userAuth->username)->find_all();
    }

    if (!empty($_POST)) {

      $model->text = Text::auto_link(strip_tags($_POST['description'],'<br><strong>'));
      $model->save();
      Message::add('success', '<strong>Změny byly uloženy.</strong>');
      Request::instance()->redirect('/'.$model->username);

    } else {

      $this->template->content = View::factory('templates/profile/edit_sets')
              ->bind('user_photos', $user_photos)
              ->bind('user_sets', $photo_sets);
    }
  }


  public function action_add_set() {

    if (!empty($_POST)) {

      $model = ORM::factory('photo_set');
      $post = $model->validate_form($_POST);

      if ($post->check()) {
      
        $model->title = $post['title'];
        $model->username = $this->userAuth->username;
        $model->save();
        Message::add('success', '<strong>Nový set byl vytvořen.</strong>');
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);
      } else {

        $informations = null;
        foreach ($post->errors('validate') as $item) {
          $informations .= '<span>' . $item . '</span>';
        }
        $this->template->infoPanelTop->errors = $post->errors('validate');
        Message::add('warning', '<strong>Nový set nebyl vytvořen.</strong> ' . $informations);
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);

      }

    } else {

      Request::instance()->redirect('/');
    }
  }

 }